﻿using System;
using EmbeddedSharp.Core;
using Zove.Runtime;

namespace Zove.Scripting.Modelling {
    /// <summary>
    /// Populate an instance for a particular class.
    /// </summary>
    [Function("populate", "Populate an instance for a particular class.",
        "c=populate(\"circle\");\n\r", "isInstanceOf(c, \"circle\")")]
    public class PopulateNode : ZoveScriptingContextFuncNode {

        /// <summary>
        /// Default constructor
        /// </summary>
        public PopulateNode()
            : base() {
            base.Value = "populate";
        }

        public override object Evaluate() {
            if (Parameters.Count < 2) {
                throw new ArgumentException(
                    "usage: populate(modelName)");
            }

            string modelName = Convert.ToString(
                Parameters[1].Evaluate());
            if (string.IsNullOrEmpty(modelName)) {
                throw new ArgumentException(
                    "usage: populate(modelName)");
            }


            string instanceName = Convert.ToString(Parameters[0].Evaluate());
            try {
                ModelInstance instance = new ModelInstance(instanceName, modelName);
                return instance;
            } catch (Exception ex) {

            }

            return null;
        }
    }
}
